<?php
namespace Admin\Controller;
use Org\Util\Date;

use Think\Controller;

class ChannelController extends BaseController {
	public function ChannelList()
	{
		$channel = M('channel_page');

		$post = $_POST;
		if (get_magic_quotes_gpc()) {
			foreach ($post as $key => $value) {
				$post[$key] = addslashes($value);
			}
		}

		$map = null;
		if (!empty($post)) {
			$where['sign'] = $where['name'] = array('like', '%'.$post['search'].'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
			if ($post['starttime'] && $post['endtime']) {
				$map['createtime'] = $map['updatetime'] = array('between', array($post['starttime'], $post['endtime']));
			}
		}

		import('ORG.Util.Page');// 导入分页类
		$pre = 12;//每页多少条
		$count = $channel->where($map)->count();// 查询满足要求的总记录数 $map表示查询条件
		$Page = new \Think\Page($count,$pre);// 实例化分页类 传入总记录数
		$show = $Page->show();// 分页显示输出
		$list = $channel->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('channel_list',html_special_chars($list));// 赋值数据集
	   
		$this->assign('page', $show);// 赋值分页输出
		$this->display("channel_list");
    }

    //新增一个渠道宣传页
	public function NewChannel()
	{
		$model = M('channel_model');
		$res = $model->getField('model_id,game_name,name');
		$model_list = array();
		foreach ($res as $key => $value) {
			$model_list[$value['game_name']][] = array('name' => $value['name'], 'id' => $value['model_id'], 'mobile' => false, 'pc' => false);
		}
		$this->assign('game_name', array_keys(($model_list)));
		$this->assign('model', $model_list);
		$this->assign('isedit', 0);
		$this->display("channel_add");
	}

	public function ChannelEdit()
	{
		$id = intval($_GET["id"]);	
		$channel = M('channel_page')->where("id=$id")->find();
		$model = M('channel_model');
		$res = $model->getField('model_id,game_name,name');
		$model_list = array();
		foreach ($res as $key => $value) {
			$model_list[$value['game_name']][] = array(
													'name' => $value['name'], 
													'id' => $value['model_id'], 
													'mobile' => ($value['model_id'] == $channel['mo_model']), //option是否被selected，下面同理
													'pc' => ($value['model_id'] == $channel['pc_model']));
		}
		$this->assign('game_name', html_special_chars(array_keys(($model_list))));
		$this->assign('model', html_special_chars($model_list));
		$this->assign('isedit', 1);
		$this->assign('channel', html_special_chars($channel));
		$this->display("channel_add");
	}
    
	public function AddChannel()
	{
		$channel = M('channel_page');
		$data = $channel->create();
		$post = $_POST;
		if (!get_magic_quotes_gpc()) {
			foreach ($post as $key => $value) {
				$post[$key] = addslashes($value);
			}
		}
		$exist = M('channel_page')->where("sign='$post[sign]'")->find();
		$data = array(
			'name' => $post['name'],
			'sign' => $post['sign'],
			'pc_model' => $post['pc_model'],
			'mo_model' => $post['mo_model'],
			'mo_ios' => $post['mo_ios'],
			'pc_ios' => $post['pc_ios'],
			'mo_andriod' => $post['mo_andriod'],
			'pc_andriod' => $post['pc_andriod'],
			'qr_ios' => $post['qr_ios'],
			'qr_andriod' => $post['qr_andriod']);
		if (1 == $post['isedit']) {//编辑状态
			$id = intval($post['id']);
			$data['updatetime'] = date("Y-m-d H:i:s", time());
			$result = $channel->where("id=$id")->save($data);
			if (false != $result) {
				$this->success('页面更新成功！', U('Admin/Channel/ChannelList', '', false, true));
			}
			else {
				$this->error("页面更新失败",  U('Admin/Channel/ChannelEdit', "id=$id", false, true));
			}
		}
		elseif (0 == $post['isedit']) {//新建状态
			if ($exist) {
				$this->error('标识'.$exist['sign'].'页面已存在！', U('Admin/Channel/NewChannel', '', false, true));
				return;
			}
			$data['createtime'] = date("Y-m-d H:i:s", time());
			$result = $channel->add($data);
			if (false != $result) {
				$this->success('页面新增成功！', U('Admin/Channel/ChannelList', '', false, true));
			}
			else {
				$this->error("页面新增失败",  U('Admin/Channel/NewChannel', '', false, true));
			}		
		}
	}

	public function Delete()
	{
		$channel = M('channel_page');
		if ('' != $_POST["radio_del"]) {
			$id = intval($_POST["radio_del"]);
			$result = $channel->delete($id);

			if (false != $result) {
				$this->success("页面删除成功 ！", U('Admin/Channel/ChannelList', '', false, true));
			}
			else {
				$this->success('没有勾选删除页面！', U('Admin/Channel/ChannelList', '', false, true));
			}
		}
		else {
			if ('' != $_POST["hidden_delete"]) {
				$arr_delete = $_POST["hidden_delete"];
				$arr_delete = substr($arr_delete, 0, strlen($arr_delete)-1); 
				$arr_delete = explode("_", $arr_delete);

				$n = 0;
				foreach ($arr_delete as $key => $value) {
					//拿到了文章ID

					$result = $channel->delete(intval($value));
					if (false != $result) {
						$n++;
					}
				}
				$this->success("页面删除成功 $n 篇！", U('Admin/Channel/ChannelList', '', false, true));
			}
			else {
				$this->success('页面勾选删除文章！', U('Admin/Channel/ChannelList', '', false, true));
			}
		}
	}
}